package leetcode101.search_problem.bfs;

import java.util.*;

/**
 * @author Synhard
 * @version 1.0
 * @Class Code6
 * @Description 47. 全排列 II
 * 给定一个可包含重复数字的序列 nums ，按任意顺序 返回所有不重复的全排列。
 * 示例 1：
 *
 * 输入：nums = [1,1,2]
 * 输出：
 * [[1,1,2],
 *  [1,2,1],
 *  [2,1,1]]
 * 示例 2：
 *
 * 输入：nums = [1,2,3]
 * 输出：[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
 *
 * 提示：
 *
 * 1 <= nums.length <= 8
 * -10 <= nums[i] <= 10
 * @tel 13001321080
 * @email 823436512@qq.com
 * @date 2021-04-07 11:18
 */
public class Code6 {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 1, 2};
        permuteUnique(nums);
    }
    public static Set<List<Integer>> set;
    public static List<List<Integer>> permuteUnique(int[] nums) {
        set = new HashSet<>();
        List<List<Integer>> res = new ArrayList<>();
        dfs(nums, 0);
        for (List<Integer> a : set) {
            res.add(a);
        }
        return res;
    }
    public static void dfs(int[] nums, int level) {
        if (level == nums.length - 1) {
            List<Integer> temp = new ArrayList<>();
            for (int num : nums) {
                temp.add(num);
            }
            set.add(temp);
            return;
        }
        for (int i = level; i < nums.length; i++) {
            swap(nums, level, i);
            dfs(nums, level + 1);
            swap(nums, level, i);
        }
    }
    public static void swap(int[] arr, int pos1, int pos2) {
        int temp = arr[pos1];
        arr[pos1] = arr[pos2];
        arr[pos2] = temp;
    }
}
